<?php
/**
 * Created by PhpStorm.
 * User: ibokan
 * Date: 2017/2/21
 * Time: 22:13
 */

namespace app\admin\controller;


class News
{
    //新闻列表
    public function index()
    {
        $arr = db("news")->select();
        return view("index", ["data"=>$arr]);
    }

    //发布新闻
    public function issue()
    {
        $arr = db("newstype")->select();
        return view("issue", ["data"=>$arr]);
    }

    //增
    public function add($title, $content, $newsType)
    {
        $publishDate = date("Y-m-d h:i:s");
        $replyCount = 0;
        $data = [
            'title'=>$title,
            'content'=>$content,
            'newsType'=>$newsType,
            'publishDate'=>$publishDate,
            'replyCount'=>$replyCount
        ];
        $res = db("news")->insert($data);
        if($res){
            echo "发布成功！<a href='index'>点击返回列表</a><hr><a href='issue'>点击继续添加新闻</a>";
        }else{
            echo "新闻发布失败！<a href='index'>点击返回列表</a><hr><a href='issue'>点击继续添加新闻</a>";
        }
    }

    //删
    public function delete($ID)
    {
        $res = db("news")->delete($ID);
        if($res){
            echo "删除成功！<a href='index'>点击返回列表</a>";
        }else{
            echo "删除失败！<a href='index'>点击返回列表</a>";
        }
    }

    //改
    public function edit($ID)
    {
        $news = db("news")->find($ID);
        $newsType = db("newstype")->select();
        return view("edit", ['news'=>$news, 'newstype'=>$newsType]);
    }

    public function save($ID, $title, $content, $newsType)
    {
        $res = db("news")->where('ID', $ID)->update([
            'title'=>$title,
            'content'=>$content,
            'newsType'=>$newsType
        ]);
        if($res){
            echo "修改成功！<a href='index'>点击返回列表</a>";
        }else{
            echo "修改失败！<a href='index'>点击返回列表</a>";
        }
    }

    //查
    public function select(){}

}


